package com.qiyu.wakaka.project.api;


import com.qiyu.wakaka.common.result.JsonResult;
import com.qiyu.wakaka.common.utils.FileUploadUtils;
import com.qiyu.wakaka.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: bear
 * @Date: 2019/2/28 17:46
 * @Description:
 */

@CrossOrigin
@RestController
@RequestMapping("/qiyu/file")
public class FileController {

    /**
     * 视频处理：视频截帧生成封面图
     *
     * @author LH_Yu
     * @Param uploadFile 上传文件
     * @Param videoPath 原视频key
     * @Param coverPicturePath 封面图上传路径及取出url的key
     * "%s|sys/saveas,o_%s,b_%s" 具体处理格式化
     */
    @PostMapping(value = "/uploadVideo")
    @ResponseBody
    public JsonResult coverPicture(HttpServletRequest request, String name) {
        String url = FileUploadUtils.coverPicture(request,name);
        return StringUtils.isEmpty(url)?JsonResult.result("url空"):new JsonResult().put("url",url);
    }


    /**
     * 上传图片并返回图片url
     * @return
     */
    @ApiOperation(value = "上传图片返回url", notes = "上传图片返回url")
    @PostMapping(value = "/uploadImg")
    @ResponseBody
    public JsonResult uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
        String url = FileUploadUtils.uploadImg(file, request);
        return StringUtils.isEmpty(url)?JsonResult.result("url空"):new JsonResult().put("fileUrl",url);
    }

    @PostMapping("/upload")
    @ResponseBody
    public JsonResult uploadFile(HttpServletRequest request, String name, Integer file_id) {
        return FileUploadUtils.uploadFiles(request,name,file_id);
    }


    @PostMapping(value = "/delete")
    @ResponseBody
    public String deleteAssetsImage(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
        return FileUploadUtils.deleteAssetsImage(request,response,model);
    }
}
